package com.admin;


import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;






public class RoleService extends MenuService {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3507151119864620152L;
	private  String table="\"Role\"";
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	protected  Role selectRole(Map pharm) {
		Role rol=new Role(); 
		String where="\"Checked\"=1";
		Iterator<String> iter = pharm.keySet().iterator();
		while(iter.hasNext()){
			   String key=iter.next();
			   String value = (String) pharm.get(key);
			   where+=" and \""+key+"\"='"+value+"'";
		}
		Map map=new HashMap();
		map.put("table", table);
		map.put("where", where);
		rol=selectrolebysql(map);
		return rol;
	}
	
	@SuppressWarnings({ "rawtypes" })
	protected   Role selectrolebysql(Map map) {
		SqlSession sqlSession = getSqlSession();
		Role rol=new Role(); 
		try {
			RoleMapper rolemp=sqlSession.getMapper(RoleMapper.class);
		rol=rolemp.selectrolebysql(map);
		
		} finally {
			sqlSession.close();
		}
		return rol;
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected   List<Role> selectrolesbysql(Map map) {
		SqlSession sqlSession = getSqlSession();
		List<Role> rol; 
		map.put("table", table);
		try {
			RoleMapper rolemp=sqlSession.getMapper(RoleMapper.class);
		rol=rolemp.selectrolesbysql(map);
		
		} finally {
			sqlSession.close();
		}
		return rol;
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected  List<Role> getrole() {
		String where="1=1";
		Map map=new HashMap();
		map.put("table", table);
		map.put("where", where);
		List<Role> rol =selectrolelistbysql(map);
		return rol;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected  List<Role> getroles(Map params) {
		String where="1=1";
		Map map=new HashMap();
		map.put("table", table);
		map.put("where", where);
		map.putAll(params);
		List<Role> rol =selectrolelistbysql(map);
		return rol;
	}
	
	
	
	/*@SuppressWarnings({ "rawtypes", "unchecked" })
	public  List<LinkedHashMap<String, String>> getroles(Map params) {
		String where="1=1";
		String fy="";
		Iterator<String> iter = params.keySet().iterator();
		while(iter.hasNext()){
			   String key=iter.next();
			   String value = (String) params.get(key);
             if(key.equals("between")) {
				   
            	   fy+=" and "+value;
			   }else  if(key.equals("like")) {
				   
            	 where+=" and "+value;
			   }
             else{
				   
				   where+=" and \""+key+"\"='"+value+"'";
			   }
		 }
		
		Map map=new HashMap();
		map.put("table", table);
		map.put("px", "\"Roleid\" desc");
		map.put("where", where);
		map.put("fy", fy);
		map.putAll(params);
		List<LinkedHashMap<String, String>> rol=selecttable(map);
		return rol;
	}*/
	
	@SuppressWarnings("rawtypes")
	protected   List<Role> selectrolelistbysql(Map map) {
		SqlSession sqlSession = getSqlSession();
		List<Role> rol; 
		try {
			RoleMapper rolemp=sqlSession.getMapper(RoleMapper.class);
		rol=rolemp.selectrolelistbysql(map);
		
		} finally {
			sqlSession.close();
		}
		return rol;
	}
	
	
	protected String saverole(Role role) {
		SqlSession sqlSession = getSqlSession();
		try {
			RoleMapper rolemp=sqlSession.getMapper(RoleMapper.class);
		int a=rolemp.insertRole(role);
		if(a==1) {
			message="新增成功";
			
		}else {
			message="新增失败";
			
		}
		
		} finally {
			sqlSession.close();
		}
		return message;
	}
	
	
	/*@SuppressWarnings("rawtypes")
	public int rolemod(Map params){//新增方法
		int isrepeat=0;
			int result = isRolerepeat(params);
			if(result>0){//有重复 不新增
				isrepeat=0;	
			}else{//无重复 执行更新方法
				SqlSession sqlSession = getSqlSession();
				try{
					RoleMapper mapper=sqlSession.getMapper(RoleMapper.class);
					mapper.insertRole(params);
				}finally{
					sqlSession.close();
				}
			}
		return isrepeat;
	}*/
	
	
	
	
	protected int isRolerepeat(String rolename){//判断角色名是否重复
		SqlSession sqlSession = getSqlSession();
		int isrole;
		try{
			RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
			List <Role> roles = mapper.selectrolesByName(rolename);
			isrole=roles.size();
		}finally{
			sqlSession.close();
		}
		return isrole;
	}
	
	//修改公司
	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected int rolemod(Map pharm){
		pharm.put("table", table);
		int result=updatetablebykey(pharm);
		return result;
	}
	
	protected List<Role> selectRoles(){
		List<Role> list ;
		SqlSession sqlSession = getSqlSession();
		try {
			RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
			list = mapper.selectRoles();
		} finally {
			sqlSession.close();
		}
		return list;
 	}

	
	//通过Usercode在User表中找到Userid 
	@SuppressWarnings("rawtypes")
	protected String selectUserid(Map map){
		String result;
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);
			result = mapper.selectUseridByUsercode(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
	
	

	
	//关闭旧角色
	@SuppressWarnings("rawtypes")
	protected int updateoldRole(Map map){
		int result;
		SqlSession sqlSession = getSqlSession();
		try {
			RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
			result = mapper.updateoldRole(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
	//开启新角色
	@SuppressWarnings("rawtypes")
	protected int updateswitchRole(Map map){
		int result;
		SqlSession sqlSession = getSqlSession();
		try {
			RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
			result = mapper.updateswitchRole(map);
		} finally {
			sqlSession.close();
		}
		return result;
	}
	
	
	

	
	protected int delectroleids(String roleids){//删除进行角色判断
		SqlSession sqlSession = getSqlSession();
		RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
		int result =1;
		try{
			String[] roleidlist=roleids.split(",");//将传入的字符串分割成数组
			for(int i=0;i<roleidlist.length;i++){
				int Roleid =Integer.parseInt(roleidlist[i]);
				result*=mapper.delectRoleByRoleid(Roleid);
				if(result == 0)  break;
			}
			if(result !=0){
				sqlSession.commit();//删除成功提交数据库操作
			}else{
				sqlSession.rollback();//删除失败不提交
			}
			
		}finally{
			sqlSession.close();
		}
		return result;
	}
	
	
	//获取角色信息
	@SuppressWarnings("rawtypes")
	protected List<Role> getrolelists(Map map){
		List<Role> list ;
		SqlSession sqlSession = getSqlSession();
		try {
			RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
			list = mapper.selectrolename(map);
		} finally {
			sqlSession.close();
		}
		return list;
	}
	
	

}
